package com.example.myjavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
import java.net.Socket;

/**
 * @author admin
 */
public class PlcCommunicationApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox root = new VBox();
        Button readDataBtn = new Button("Read Data from PLC");
        Button writeDataBtn = new Button("Write Data to PLC");
        root.getChildren().addAll(readDataBtn, writeDataBtn);

        readDataBtn.setOnAction(event -> {
            try (Socket socket = new Socket("plc_ip", 12345);
                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                out.println("READ_DATA");
                String response = in.readLine();
                System.out.println("Received from PLC: " + response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        writeDataBtn.setOnAction(event -> {
            try (Socket socket = new Socket("plc_ip", 12345);
                 PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
                out.println("WRITE_DATA");
                System.out.println("Command sent to PLC");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("PLC Communication");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
